package com.nineclock.document.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.document.mapper.DocFileHistoryMapper;
import com.nineclock.document.pojo.DocFileHistory;
import com.nineclock.document.service.DocFileHistoryService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysCompanyUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DocFileHistoryServiceImpl implements DocFileHistoryService {

    @Autowired
    private DocFileHistoryMapper fileHistoryMapper;

    @Autowired
    private SysCompanyUserFeign companyUserFeign;

    /**
     * 查询文件历史记录
     * */
    @Override
    public List<DocFileHistoryDto> queryHistory(Long fileId) {
        if (fileId!=null){
            LambdaQueryWrapper<DocFileHistory> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(DocFileHistory::getFileId,fileId);
            List<DocFileHistory> docFileHistoryList = fileHistoryMapper.selectList(wrapper);
            List<DocFileHistoryDto> fileHistoryDtoList = BeanHelper.copyWithCollection(docFileHistoryList, DocFileHistoryDto.class);
            List<DocFileHistoryDto>list=new ArrayList<>();
            for (DocFileHistoryDto docFileHistoryDto : fileHistoryDtoList) {
                SysCompanyUserDTO companyUserDTO = companyUserFeign.queryCompanyUserById(docFileHistoryDto.getOperateUserId()).getData();
                docFileHistoryDto.setOperateUserId(companyUserDTO.getId());
                docFileHistoryDto.setFileId(fileId);
                docFileHistoryDto.setUserName(companyUserDTO.getUserName());
                list.add(docFileHistoryDto);
            }

            return list;
        }
        return null;
    }


}
